# -*- coding: UTF8 -*-
import json
import os

from selenium import webdriver
import math
import time
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

def main(save_path,html_url):
    # 设置打印机的纸张大小、打印类型、保存路径等
    chrome_options = webdriver.ChromeOptions()
    settings = {
        "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": ""
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "isHeaderFooterEnabled": False,

        # "customMargins": {},
        # "marginsType": 2,#边距（2是最小值、0是默认）
        # "scaling": 100,
        # "scalingType": 3,
        # "scalingTypePdf": 3,
        # "isLandscapeEnabled": True,  # 若不设置该参数，默认值为纵向
        "isCssBackgroundEnabled": True,
        "mediaSize": {
            "height_microns": 297000,
            "name": "ISO_A4",
            "width_microns": 210000,
            "custom_display_name": "A4"
        },
    }

    chrome_options.add_argument('--enable-print-browser')
    # chrome_options.add_argument('--headless') #headless模式下，浏览器窗口不可见，可提高效率
    

    if not os.path.exists(save_path):
        print("保存目录不存在,创建目录"+save_path)
        os.makedirs(save_path)
    
    if os.path.isabs(save_path)==False:
        print("相对路径转换为绝对路径"+save_path)
        save_path=os.path.abspath(save_path)
        print("相对路径转换为绝对路径"+save_path)

    prefs = {
        'printing.print_preview_sticky_settings.appState': json.dumps(settings),
        # 此处填写你希望文件保存的路径,可填写your file path默认下载地址
        'savefile.default_directory': save_path
    }

    chrome_options.add_argument('--kiosk-printing')  # 静默打印，无需用户点击打印页面的确定按钮
    chrome_options.add_experimental_option('prefs', prefs)

    driver = webdriver.Chrome(options=chrome_options)
    driver.set_script_timeout(200)
    driver.get(html_url)
    driver.maximize_window()  # 浏览器最大化

    # 移除多余部分打印
    # window.document.body.innerHTML=document.getElementById("page-content").innerHTML
    # document.getElementsByTagName("mp-common-profile")[0].remove()
    # document.body.scrollHeight 高度 document.body.clientHeight window.innerHeight
    # browser.find_element_by_tag_name('body').send_keys(Keys.Space)
    # time.sleep(3)
    bodyHeight= driver.execute_script("return document.body.scrollHeight")
    screenHeight=driver.execute_script("return window.innerHeight")
    times=math.ceil( bodyHeight/screenHeight)+3
    for i in range(times):
        driver.find_element_by_tag_name('body').send_keys(Keys.SPACE)
        sleep(3)
    
    #删除文件重新保存
    title= driver.execute_script("return document.title")
    
    
    #去除特殊字符，只保留汉子，字母、数字
    import re
    #去除不可见字符
    prue_title=re.sub("[^\u4e00-\u9fa5^a-z^A-Z^0-9]",'',title)
    print(prue_title)
    
    #获取文章发布时间
    publish_time=""
    
    p_time=driver.execute_script("return document.getElementById('publish_time').innerHTML")
    publish_time = time.strftime("%Y_%m_%d_", time.strptime(p_time, "%Y-%m-%d %H:%M"))

    print("发布时间%s"%(publish_time))
    pdf_path=save_path+os.sep+publish_time+prue_title+".pdf"
    print("文件预计保存路径:"+pdf_path)
    if os.path.exists(pdf_path):
        print("文件存在,删除文件:"+save_path)
        os.remove(pdf_path)
    
    try:
        driver.execute_script('window.document.body.innerHTML=document.getElementById("page-content").innerHTML;')
        driver.execute_script('document.getElementsByTagName("mp-common-profile")[0].remove();')
    except:
        pass
    driver.execute_script('document.body.style.backgroundColor="#fff"')   
    driver.execute_script('document.title="%s";'%(publish_time+prue_title))
    
    try:
        driver.execute_script('window.print();')  
    except:
        pass
    # 利用js修改网页的title，该title最终就是PDF文件名，利用js的window.print可以快速调出浏览器打印窗口，避免使用热键ctrl+P
    # driver.close()#关闭当前页
    # driver.quit()
if __name__ == '__main__':
    import sys
    save_path = sys.argv[1]
    html_url = sys.argv[2]
    main(save_path,html_url)
# py.exe C:\Users\GUOKA\Desktop\app\wechat-history\print_html2pdf.py './gzh_pdf/淘淘升学' 'http://mp.weixin.qq.com/s?__biz=MzI2Mzc0MjI0MQ==&mid=2247502111&idx=1&sn=be43177cd8dc5ef35ccf29425e1573d3&chksm=eab5bf51ddc236475ce6e3aebbc0f280ab972eb267b8866a7eec91f4dc83ea2b2d9ff5525255#rd'